<template>
    <!-- 
    <b-modal ref="my-modal" hide-footer title="Using Component Methods">
     <div class="d-block text-center">
       <h3>Hello From My Modal!</h3>
     </div>
     <b-button class="mt-3" variant="outline-danger" block @click="hideModal">Close Me</b-button>
     <b-button class="mt-2" variant="outline-warning" block @click="toggleModal">Toggle Me</b-button>
   </b-modal> 
    -->
    <!--
        body-class="modalStyle" 表示样式
        size="lg" 表示模态窗口大小 xl,lg,sm,full
        hide-footer 表示隐藏底部按钮
        hide-header 表示隐藏头部内容
        no-close-on-backdrop 表示鼠标点击背景不可关闭
        hide-header-close 表示隐藏头部关闭按钮
        centered 居中
        ok-title=“确定” 
        cancel-title=取消
        @ok="handleOk" 
        @cancel="handleCancel"
        scrollable 滚动条
        hide-backdrop 隐藏背景
        wrapClassName="ant-modal-cust-warp" 
        style="top:5%;height: 85%;overflow-y: hidden" 样式
    -->
    <b-modal ref="my-modal" size="lg" title="编辑功能" scrollable no-close-on-backdrop>
        <div class="d-block text-center">
            <!--begin::div-->
            <div class="m-form m-form--fit text-left m-form--group-seperator-dashed">
                <div class="m-portlet__body">
                    <div class="form-group m-form__group row">
                        <label class="col-lg-2 col-form-label" jEhcLang="resources_title">隶属模块</label>
                        <div class="col-lg-6">
                            <div class="form-group input-group">
                                <input class="form-control" type="hidden" v-model="oauthFunctionInfoForm.resourcesId"/>
                                <input class="form-control" type="hidden" v-model="oauthFunctionInfoForm.id"/>
                                <input class="form-control" type="text" readonly="readonly" placeholder="请选择" v-model="oauthFunctionInfoForm.resourcesName"/>
                                <span class="input-group-btn">
                                    <button class="btn btn-default" type="button" @click="xtMenuinfoSelect(1)">
                                        选择模块
                                    </button>
                                </span>
                            </div>
                        </div>
                    </div>
                    <div class="form-group m-form__group row">
                        <label class="col-lg-2 col-form-label">功能名称</label>
                        <div class="col-lg-4">
                            <input class="form-control placeholder-no-fix" type="text" placeholder="请输入" v-model="oauthFunctionInfoForm.name"/>
                        </div>
                    </div>
                    <div class="form-group m-form__group row">
                        <label class="col-lg-2 col-form-label">请求地址</label>
                        <div class="col-lg-8">
                            <input class="form-control" type="text" placeholder="请输入" v-model="oauthFunctionInfoForm.url"/>
                        </div>
                    </div>
                    <div class="form-group m-form__group row">
                        <label class="col-lg-2 col-form-label">按钮方法</label>
                        <div class="col-lg-8">
                            <input class="form-control" type="text" placeholder="请输入" v-model="oauthFunctionInfoForm.method"/>
                        </div>
                    </div>
                    <div class="form-group m-form__group row">
                        <label class="col-lg-2 col-form-label">是否拦截</label>
                        <div class="col-lg-2">
                            <el-select maxlength="20" v-model="oauthFunctionInfoForm.isFilter" placeholder="请选择">
                                <el-option
                                    v-for="item in [{value:0,label:'否'},{value:1,label:'是'}]"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </div>
                        <label class="col-lg-2 col-form-label">数据权限</label>
                        <div class="col-lg-2">
                            <el-select maxlength="20" v-model="oauthFunctionInfoForm.isAuthority" placeholder="请选择">
                                <el-option
                                    v-for="item in [{value:0,label:'是'},{value:1,label:'否'}]"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </div>
                    </div>
                    <div class="form-group m-form__group row">
                        <label class="col-lg-2 col-form-label" jEhcLang="status">是否可用</label>
                        <div class="col-lg-2">
                            <el-select maxlength="20" v-model="oauthFunctionInfoForm.status" placeholder="请选择">
                                <el-option
                                    v-for="item in [{value:0,label:'是'},{value:1,label:'否'}]"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <template slot="modal-footer">
            <!--自定义按钮-->
            <button type="button" class="btn btn-light-danger font-weight-bold mr-2" block @click="hideModal">
                <i class="icon-xl fa fa-times"></i>取消
            </button> 
            <button type="button" class="btn btn-light-success font-weight-bold mr-2" @click="updateOauthFunctionInfo">
                <i class="icon-xl fas fa-save"></i> 保存
            </button>
        </template>
        <OauthFunctionTreeSelect ref="oauthFunctionTreeSelectRef" @change="setFunctionSelect"></OauthFunctionTreeSelect>
    </b-modal>        
</template>
<script>
import apiUtil from "@/core/util/apiUtil.js"; 
import { handleNotify,handleAlert,handleConfirm,showWating,closeWating} from "@/core/util/jehcUtil.js";
import OauthFunctionTreeSelect from "@/view/oauth/oauth-function-info/oauth-function-info-tree-select.vue";
export default {
    data(){
        return {    
            oauthFunctionInfoForm:{
                id:"",
                resourcesId: "",
                resourcesName:"",
                name:'',
                url:"",
                method:"",
                isFilter:1,
                isAuthority:1,
                status:0,
            },
            sysModeList:[]
        }
    },
    components: {
        OauthFunctionTreeSelect,
    },
    watch: {
    },
    mounted() {
    },
    methods: {
        setFunctionSelect(data){
            this.oauthFunctionInfoForm.resourcesId = data.id;        
            if(data.name == null || data.name ==""){
                this.oauthFunctionInfoForm.resourcesName="暂无"
            }else{
                this.oauthFunctionInfoForm.resourcesName=data.name
            }   
        },
        xtMenuinfoSelect(type){
            this.$refs.oauthFunctionTreeSelectRef.showModal(type);
        },
        updateOauthFunctionInfo(){
            if(this.oauthFunctionInfoForm.resourcesId === ""){
                handleAlert("请选择模块", "warning", 3)
                return;
            }
            if(this.oauthFunctionInfoForm.name === ""){
                handleAlert("请输入名称", "warning", 3)
                return;
            }
            if(this.oauthFunctionInfoForm.url === ""){
                handleAlert("请输入请求地址", "warning", 3)
                return;
            }
            if(this.oauthFunctionInfoForm.method === ""){
                handleAlert("请输入按钮方法", "warning", 3)
                return;
            }
            if(this.oauthFunctionInfoForm.isFilter === ""){
                handleAlert("请选择是否拦截", "warning", 3)
                return;
            }
            if(this.oauthFunctionInfoForm.isAuthority === ""){
                handleAlert("请选择是否数据权限", "warning", 3)
                return;
            }
            // 保存前提示
            this.$confirm("确定保存?", "提示", {type: "warning",}).then(() => {
                apiUtil.update(process.env.VUE_APP_OAUTH_API+"/oauthFunctionInfo/update", this.oauthFunctionInfoForm).then(({ data }) => {
                    if(data.success){
                        handleAlert("编辑成功", "success", 3);
                        this.hideModal();//关闭窗体
                        this.$emit("change",data);//组件传值，即向父级模态框中传递值，采用change方式，目标刷新主列表使用
                    }else {
                        handleAlert("编辑失败", "error", 3);
                    }            
                });
            }).catch(()=>{});//注意这里，这里是重点！！！;   
        },
        showModal(id) {
            this.restForm();
            this.$refs['my-modal'].show();
            this.getOauthFunctionInfoDetail(id);
        },
        hideModal() {
            this.$refs['my-modal'].hide()
        },
        toggleModal() {
            // 当模态已隐藏时，我们传递要返回焦点的按钮的ID
            this.$refs['my-modal'].toggle('#toggle-btn')
        },
        restForm(){
            //重置
            Object.assign(this.$data.oauthFunctionInfoForm, this.$options.data().oauthFunctionInfoForm);
        },
        getOauthFunctionInfoDetail(id){
            apiUtil.get(process.env.VUE_APP_OAUTH_API+"/oauthFunctionInfo/get/"+id).then(({ data }) => {
                this.oauthFunctionInfoForm = data.data;                      
            });
        }
   }
}
</script>